package com.chinasolf.api;

import com.chinasoft.model.hosp.Department;
import com.chinasoft.model.hosp.Hospital;
import com.chinasoft.model.hosp.Schedule;
import com.chinasolf.exception.YyghException;
import com.chinasolf.result.Result;
import com.chinasolf.service.DepartmentService;
import com.chinasolf.service.HospitalService;
import com.chinasolf.service.HospitalSetService;
import com.chinasolf.service.ScheduleService;
import com.chinasolf.utils.HttpRequestHelper;
import com.chinasolf.utils.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String)map.get("limit"));
        Page<Schedule> pageModel = scheduleService.selectPage(page,limit);
        return Result.ok(pageModel);
    }


    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        scheduleService.saveSchedule(map);
        return Result.ok();
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取医院和科室编号
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");
        //调用方法的删除
        departmentService.removeDept(hoscode, depcode);
        return Result.ok();


    }


    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String) map.get("limit"));
        //调用方法分页查询
        Page<Department> pageMode1 = departmentService.selectPage(page, limit);
        return Result.ok(pageMode1);

    }

    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        departmentService.saveDept(map);
        return Result.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传医院
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //1.获取医院系统提交过来数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //校验功能
        String signHosp = (String) map.get("sign");
        String hoscode = (String) map.get("hoscode");
        String singKeyYygh = hospitalSetService.getSignKey(hoscode);
        //比对传递签名秘钥  和 平台保存签名秘钥是否一样
        // 平台里面存储签名秘钥进行MD5加密之后进行比对
        String encryptYygh = MD5.encrypt(singKeyYygh);
        if (!encryptYygh.equals(signHosp)) {
            throw new YyghException(20001, "签名校验失败");
        }

        //base64传输过程中“+”转换为了“ ”，因此我们要转换回来，否则图标不显示
        String logoData = (String) map.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        map.put("logoData", logoData);

        //3 调用方法把提交过来数据添加mongodb里面
        hospitalService.saveHosp(map);

        return Result.ok();
    }


}
